package iyoga;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

public class Card {
	private int card_id; //卡号
	private int user_id; //用户id
	private String card_type;//卡类型
	private Date start_time;//办卡时间
	private int user_times;//使用次数
	private int state;//是否可用
	    //给用户添加卡 1用户姓名 2卡类型 3次卡的次数（年卡为-1）
		public static void add_card(String name,String type,int count){
			if(!User.is_register(name)){
				System.out.println("Error:UserName Not Found!");
				return;
			};
			int user_id = 0;
			Connection conn=JDBCUtil.getConnection();
			String id_sql="select user_id from user where user_name='"+name+"'";
			Statement stmt;
			try {
				stmt = conn.createStatement();
				ResultSet rs=stmt.executeQuery(id_sql); 
				while(rs.next()){
					user_id = rs.getInt("user_id");
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			String sql="insert into card(user_id,card_type,user_times) values('"+user_id+"','"+type+"' ,' "+count+"')";
			
			try {
				stmt = conn.createStatement();
				stmt.executeUpdate(sql);   
				if(type.equals("年卡")){
				System.out.println("A new 年卡 added to"+name+"("+user_id+")");
				}
				if(type.equals("次卡")){
					System.out.println("A new 次卡  with "+count+"times added to"+name);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			
			
		}
		public int getCard_id() {
			return card_id;
		}
		public void setCard_id(int card_id) {
			this.card_id = card_id;
		}
		public int getUser_id() {
			return user_id;
		}
		public void setUser_id(int user_id) {
			this.user_id = user_id;
		}
		public String getCard_type() {
			return card_type;
		}
		public void setCard_type(String card_type) {
			this.card_type = card_type;
		}
		public Date getStart_time() {
			return start_time;
		}
		public void setStart_time(Date start_time) {
			this.start_time = start_time;
		}
		public int getUser_times() {
			return user_times;
		}
		public void setUser_times(int user_times) {
			this.user_times = user_times;
		}
		public int getState() {
			return state;
		}
		public void setState(int state) {
			this.state = state;
		}
	

	//getter and setter	

}
